package com.xsky.logic;

import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xsky.converter.XStreamClobConverter;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.TabEngineer;
import com.xsky.operation.vo.TabRoleAccess;
import com.xsky.operation.vo.TabUserRole;

public class LogonLogic
{
	public String logon(String username, String password)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		String resultStr = "<root>";
		List list = (baseDao.findByHql("from TabEngineer model where model.username='"+username+"' and model.password='"+password+"'"));
		if(list != null)
		{
			TabEngineer engineer = (TabEngineer)list.get(0);
			XStream xs = new XStream(new DomDriver());
			String defaultFormat = "yyyy-MM-dd";
			String[] acceptableFormat = {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd","yyyy-MM"};
			xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
			xs.registerConverter(new XStreamClobConverter());
			xs.alias("userInfo", TabEngineer.class);
			resultStr += xs.toXML(engineer);
			
			String engineerId = engineer.getId();
			List roleList = baseDao.findByHql("from TabUserRole model where model.userId='"+engineerId+"'");
			resultStr += "<access>";
			for(int j = 0; j < roleList.size(); j++)
			{
				TabUserRole userRole = (TabUserRole)roleList.get(j);
				String roleId = userRole.getRoleId();
				List powerList = baseDao.findByHql("from TabRoleAccess model where model.roleId='"+roleId+"'");
				for(int m = 0; m < powerList.size(); m++)
				{
					TabRoleAccess roleAccess = (TabRoleAccess)powerList.get(m);
					String accessCode = roleAccess.getAccessCode();
					resultStr += "<code>"+accessCode+"</code>";
				}
			}
			resultStr += "</access>";
		}
		else
		{
			return "";
		}
		resultStr+="</root>";
		return resultStr;
	}
}
